package com.railway.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.railway.bean.Contact;
import com.railway.dao.ContactDao;
import com.railway.util.DBUtil;

public class ContactDaoImpl implements ContactDao {
	
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	@Override
	public int insertContact(int contact_id,Contact contact) {
		String sql = "insert into contact(contact_id,user_id,contact_name,contact_phone,contact_id_type,contact_id_num,contact_passenger_type,is_exist)"
				+ " values(?,?,?,?,?,?,?,?)";
		return jt.update(sql,contact_id,contact.getUser_id(),contact.getContact_name(),contact.getContact_phone(),
				contact.getContact_id_type(),contact.getContact_id_num(),contact.getContact_passenger_type(),1);
	}

	@Override
	public int deleteContact(int user_id, int contact_id) {
		String sql = "update contact into is_exist = 0 where user_id = ? and contact_id = ?";
		return jt.update(sql, user_id,contact_id);
	}

	@Override
	public List<Contact> showContactByUser(int user_id) {
		String sql = "select * from contact where user_id = ? and is_exist = 1";
		return jt.query(sql, new BeanPropertyRowMapper<Contact>(Contact.class),user_id);
	}

	@Override
	public List<Contact> showContactByAdmin(int user_id) {
		String sql = "select * from contact where user_id = ?";
		return jt.query(sql, new BeanPropertyRowMapper<Contact>(Contact.class),user_id);
	}

	@Override
	public int recoverContact(int user_id, int contact_id) {
		String sql = "update contact into is_exist = 1 where user_id = ? and contact_id = ?";
		return jt.update(sql, user_id,contact_id);
	}

	@Override
	public List<Contact> selectContactByIdAndName(int user_id, String user_name) {
		String sql = "select * from contact where user_id = ? and is_exist = 1 and user_name = ?";
		return jt.query(sql, new BeanPropertyRowMapper<Contact>(Contact.class),user_id,user_name);
	}

	@Override
	public int selectContactNum(int user_id) {
		String sql = "select count(1) from contact where user_id = ? group by user_id";
		return jt.query(sql, new BeanPropertyRowMapper<Integer>(Integer.class),user_id).get(0);
	}
	
	
}
